package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("user")
@Controller
public class Usercontroller {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("Return")
    public String Return(){
        UserMapper userMapper;
        return "redirect:/user/userList";
    }
    @RequestMapping("userList")
/**显示所有数据  */
    public String LisT(ModelMap map) {
        List<User> list = userMapper.selAll();
        for (User user : list) {
            System.out.println(user.getTime());
            System.out.println(user.getMan());
            System.out.println(user.getNumber());
            System.out.println(user.getWater());
            System.out.println(user.getId());
        }
        map.put("userList", list);
        return "index";
    }

    @RequestMapping("add")
    /**进入添增页面的跳转*/
    public String add(){
        return "/User/add";
    }
    @RequestMapping("save")
    public String save(User user){
        userMapper.Save(user);
        return "redirect:/user/Return";
    }

    @RequestMapping("setAll")
    public String setAllList(ModelMap map){
        List<User> list= userMapper.selAll();
        for (User user:list){
            System.out.println(user.getTime());
            System.out.println(user.getMan());
            System.out.println(user.getNumber());
            System.out.println(user.getWater());
            System.out.println(user.getId());
        }
        map.put("setAllList",list);
        return "/setAll";
    }
/**ajax 的获取全部数据的方法*/
    @RequestMapping("ajaxtest")
    @ResponseBody
    public String ajaxtest(){
        return "xxxxx";
    }


    @RequestMapping("ReturnSetAll")
    public String ReturnSetAll(){
        UserMapper userMapper;
        return "redirect:/user/setAll";
    }
    @RequestMapping("/update/{id}")
/**进入修改页面的跳转*/
    public String update(@PathVariable("id") int id,ModelMap map){
        User user=userMapper.GetUserInfo(id);
        map.put("user",user);
        return "User/Update";
    }
    @RequestMapping("ReallyUpdate")
    /**数据修改后，提交页面的修改*/
    public  String ReallyUpdate(User user){
        userMapper.GetUpdate(user);
        return "redirect:/user/ReturnSetAll";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") int id){
        userMapper.DeleteByid(id);
        return "redirect:/user/ReturnSetAll";
    }


}
